﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using RHBOQry = Texfina.BOQry.Rh;

namespace RH
{
   public partial class wfFeriadoBnd : EditPage
   {
      public const int _GRDLIST_STANULADO = 2;
      public const int _GRDLIST_FECHA = 1;

      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
         this.LockControls();

         //if (this.DataSource != null)
         //{
         //   grdList.DataSource = this.DataSource;
         //   grdList.DataBind();
         //}

      }

      public override void LoadInitialSettings()
      {
          this.FormEditAction = 0;
          //this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
          this.SetInitialTitle("Feriados");
      }

      public override void DataToControls()
      {
         EFeriado objE = new EFeriado();
         objE.IdPeriodo=Session[ConstSessionVar.PERIODO].ToString();
         objE.FeFeriado =DateTime.Now;
        
         IBOQuery objBO = WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Feriado));

         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      public override void RegisterGeneralScripts()
      {

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validate" + this.bttAgregar.ID + "(){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeFeriado.ClientID + "','Seleccione una fecha.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = gridFindCellValueInRow('" + grdList.ClientID + "',new Array('" + _GRDLIST_FECHA + "'), new Array(document.getElementById('" + this.txtfeFeriado.ClientID + "').value),'La fecha ya Existe');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");


         sb.AppendLine("return true;");
         sb.AppendLine("}");

         sb.AppendLine("function validateEditForm(key){");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         //bttListAD.OnClientClick = "return validate" + bttListAD.ID + "();";

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

         this.bttAgregar.OnClientClick = "return validate" + bttAgregar.ID + "();";

      }

      public override void LockControls()
      {

         //switch (this.FormEditAction)
         //{
         //   case FormEditAction.Edit:
         //      txtidLogon.Attributes.Add("readonly", "true");
         //      bttListAD.Attributes.Add("disabled", "true");
         //      break;
         //}

         //if (User.Identity.AuthenticationType == ConstAuthenticationType.LDAP)
         //{
         //   txtidLogon.Attributes.Add("readonly", "true");
         //   txtdsNombres.Attributes.Add("readonly", "true");
         //}

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfFeriadoBnd.aspx");
               break;
         }

      }

      public override void SaveData(String key)
      {
         //if (this.ValidateData() == false)
         //   return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();

         IBOUpdate objBO = WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Feriado));

         string[] strRet = objBO.UpdateData(objE);
         string strMsg = "";

         switch (this.FormEditAction)
         {

            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se han actualizado los cambios ";
               break;
            default:
               //strMsg = "Se ha modificado el Usuario : " + txtidLogon.Text;
               break;
         }

         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfFeriadoBnd.aspx");
         ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "msgPB", "MessageOnPostback();", true);
      }

      public override Object CreateEntitySet()
      {
         EFeriado objE = new EFeriado();

         //if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
         //   objE.EntityState = EntityState.Added;
         //else
         //{
         //   IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Feriado));
         //   objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         //   objE.IdPlanilla = txtidPlanilla.Text;
         //   objE = (EPlanilla)objBO.GetByKey(objE);

         //   if (this.FormEditAction == FormEditAction.Edit)
         //      objE.EntityState = EntityState.Modified;
         //   if (this.FormEditAction == FormEditAction.Delete)
         //      objE.EntityState = EntityState.Deleted;

         //}

         //objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         //WebHelper.ControlToEntity(txtidPlanilla, objE, "IdPlanilla");
         //WebHelper.ControlToEntity(txtdsPlanilla, objE, "DsPlanilla");
         //WebHelper.ControlToEntity(txtnuProcAnual, objE, "NuProcAnual");

         //if (this.FormEditAction == FormEditAction.Delete)
         //{
         //   objE.StAnulado = ConstFlagEstado.ACTIVADO;
         //}
         //else
         //{
         //   objE.StAnulado = ConstFlagEstado.DESACTIVADO;
         //}

         DataTable dt = (DataTable)this.DataSource;

         //this.ClearDeletedRow(this.DataSource, "stAnulado");

         IList<EFeriado> colE = new List<EFeriado>();

         for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
         {
            DataTable dtChange = null;
            if (intTypeUpd == 0)
               dtChange = dt.GetChanges(DataRowState.Added);
            else
               dtChange = dt.GetChanges(DataRowState.Modified);


            if (dtChange != null)
            {
               foreach (DataRow drDet in dtChange.Rows)
               {
                  EFeriado objEDet = DataHelper.CopyDataRowToEntity<EFeriado>(drDet, typeof(EFeriado));

                  if (drDet.RowState == DataRowState.Added)
                     objEDet.EntityState = EntityState.Added;

                  if (drDet.RowState == DataRowState.Modified)
                  {
                        objEDet.EntityState = EntityState.Modified;
                  }

                  colE.Add(objEDet);
               }
            }
         }

         EFeriadoSet objESet = new EFeriadoSet();

         objESet.ColFeriado = colE;

         return objESet;

      }

      protected void bttAgregar_Click(object sender, EventArgs e)
      {
         DataTable dt = (DataTable)this.DataSource;
         DataRow dr = null;

   

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
         {
            dr = dt.NewRow();
         }
         else
         {
            dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
         }

         if (this.FormEditAction!= FormEditAction.Delete)
         {
            dr["idPeriodo"] = Session[ConstSessionVar.PERIODO].ToString();
            dr["feFeriado"] = this.txtfeFeriado.Text;
            dr["idMes"] = "0"+this.txtfeFeriado.Text.Substring(3, 2).ToString();

         }

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
         {
            dt.Rows.Add(dr);
         }


         this.GridSetDataBinding(grdList, dt);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

         switch (e.CommandName)
         {
            //case ConstGridRowCommand.GRIDEDIT:
            //   this.lblTitlePopup.Text = "Detalle Planilla (Editar)";
            //   this.GridEditAction = GridEditAction.Edit;
            //   this.mdlPopup.Show();
            //   this.LoadDataEditPopup(grdList, this.EditRowIndex);
            //   this.RegisterEditPopupScripts();
            //   break;
            case ConstGridRowCommand.GRIDDELETE:
               //this.lblTitlePopup.Text = "Detalle Planilla (Dar de Baja)";
               //this.GridEditAction = GridEditAction.Delete;
               //this.mdlPopup.Show();
               //this.LoadDataEditPopup(grdList, this.EditRowIndex);
               //this.RegisterEditPopupScripts();
                DataTable dt = (DataTable)this.DataSource;
                DataRow dr = null;
                dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
                dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
                this.GridSetDataBinding(grdList, dt);
                //grdList.DataBind();
             
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            { e.Row.ForeColor = System.Drawing.Color.Red; }

         }
      }


   }
}
